package com.stu.manager.organization.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stu.manager.config.Result;
import com.stu.manager.handler.LoginHandler;
import com.stu.manager.organization.entity.SysClassinfo;
import com.stu.manager.organization.entity.SysProfessional;
import com.stu.manager.organization.entity.dto.AddClassDto;
import com.stu.manager.organization.entity.dto.ClassInfoDto;
import com.stu.manager.organization.entity.dto.ProfessionalDto;
import com.stu.manager.organization.entity.dto.TeacherManage;
import com.stu.manager.organization.entity.vo.OneLevel;
import com.stu.manager.organization.service.SysClassinfoServiceImpl;
import com.stu.manager.personnel.entity.SysStudent;
import com.stu.manager.personnel.mapper.SysStudentMapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-02-13
 */
@RestController
@RequestMapping("/api/classinfo")
@CrossOrigin
public class SysClassinfoController {
    @Autowired
    private SysClassinfoServiceImpl classinfoService;

    @Autowired
    private LoginHandler loginHandler;

    @Resource
    private SysStudentMapper studentMapper;


    @PostMapping("pageClassInfo/{current}/{limit}")
    @ApiOperation("模糊查询加分页展示班级")
    public Result pageClassInfo(@PathVariable Long current,
                                @PathVariable Long limit,
                                @RequestBody(required = false) ClassInfoDto classInfoDto, HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        Page<SysClassinfo> pageClassInfo = new Page<>(current, limit);
        QueryWrapper<SysClassinfo> wrapper = new QueryWrapper<>();
        String departmentName = classInfoDto.getDepartmentName();
        String professionalName = classInfoDto.getProfessionalName();

        if (!StringUtils.isEmpty(professionalName)) {
            wrapper.like("professional_name", professionalName);
        }
        if (!StringUtils.isEmpty(departmentName)) {
            wrapper.like("department_name",departmentName);
        }

        IPage<SysClassinfo> sysProfessionalIPage = classinfoService.pageClassInfo(pageClassInfo, wrapper);
        List<SysClassinfo> records = sysProfessionalIPage.getRecords();
        long total = sysProfessionalIPage.getTotal();
        return Result.ok().data("total", total).data("records", records);
    }

    @GetMapping("getClassLevel")
    @ApiOperation("查询学院专业一二级分类")
    public Result getClassLevel(HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        List<OneLevel> classLevel = classinfoService.getClassLevel();
        return Result.ok().data("oneLevelList",classLevel);
    }

    @PostMapping("addClassInfo")
    @ApiOperation("添加班级信息")
    public Result addClassInfo(@RequestBody AddClassDto addClassDto,HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        Result result = classinfoService.addClassInfo(addClassDto);
        return result;
    }

    @GetMapping("getClassInfoById/{id}")
    @ApiOperation("查询一个班级信息")
    public Result getClassInfoById(@PathVariable String id,HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        SysClassinfo classInfoById = classinfoService.getClassInfoById(id);
        return Result.ok().data("item",classInfoById);
    }


    @PostMapping("updateClassInfo")
    @ApiOperation("修改班级信息")
    public Result updateClassInfo(@RequestBody SysClassinfo classinfo,HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        Result result = classinfoService.updateClassInfo(classinfo);
        return result;
    }


    @DeleteMapping("delClassInfo/{id}")
    @ApiOperation("删除班级信息")
    public Result delClassInfo(@PathVariable String id,HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        QueryWrapper<SysClassinfo> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        SysClassinfo one = classinfoService.getOne(wrapper);
        String professionalName = one.getProfessionalName();
        Integer classGrade = one.getClassGrade();
        Integer classCode = one.getClassCode();

        QueryWrapper<SysStudent> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("professional_name",professionalName).eq("hiredate",classGrade).eq("class_code",classCode);
        Integer count = studentMapper.selectCount(wrapper1);
        if(count>0){
            return Result.error().message("班级存在学生，请先处理学生信息！");
        }

        boolean remove = classinfoService.remove(wrapper);
        if (remove) {
            return Result.ok().message("删除成功！");
        } else {
            return Result.error().message("删除失败！");
        }
    }

    @PostMapping("addTeacherManage")
    @ApiOperation("添加或修改管理教师")
    public Result addTeacherManage(@RequestBody TeacherManage teacherManage,HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        Result result = classinfoService.addTeacherManage(teacherManage);
        return result;
    }

    @GetMapping("getClassCode/{hiredate}/{professionalName}")
    @ApiOperation("根据专业名及入学年份获取班级")
    public Result getClassCode(@PathVariable String hiredate,@PathVariable String professionalName,HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        Result result = classinfoService.getClassCode(hiredate, professionalName);
        return result;
    }

}

